
import random

import django_redis
from captcha.image import ImageCaptcha
from django.http import JsonResponse, HttpResponse
from django.views import View


class Imgcode(View):
    def get(self,request,uuid):

        #获取请求参数
        client=request.GET.get('uuid')


        #校验请求参数
        if client is None:
            return JsonResponse({'code':4001,'errormsg':'参数不完整'})


       #生成图片验证码
        num=random.randint(1000,9999)
        str_num=str(num)
        generate_img=ImageCaptcha()
        imgcode=generate_img.generate(chars=str_num)

       #保存图片验证码
        redis_conn=django_redis.get_redis_connection('verify_code')
        redis_conn.setex('img_%s'%uuid,60,imgcode)



       #返回响应给前端
        return  HttpResponse(imgcode,content_type='image/png')